/**
 * @name insertInput
 * @description 插入输入框
 * @param {Object} curentShape - 当前形状
 * @param {Object} position - 鼠标位置
 * @param {Object} shapes - 形状数组
 * @param {number} left - 输入框插入位置
 * @param {number} top - 输入框插入位置
 */
function insertInput({ curentShape, position, shapes, left, top }) {
  const input = document.createElement("input");
  input.type = "text";
  input.style.position = "absolute";
  input.style.padding = "0px";
  input.style.margin = "0px";
  input.style.width = curentShape.width * 0.8 + "px";
  input.style.border = 'none'
  input.style.outline = 'none'
  input.value = curentShape.text;
  document.body.appendChild(input);
  console.dir(input);
  input.style.top = top - input.clientHeight / 2 + position.top + "px";
  input.style.left = left - input.clientWidth / 2 + position.left + "px";
  input.focus();
  input.onblur = () => {
    const value = input.value;
    curentShape.text = value;
    shapes.render(ctx);
    input.remove();
  };
}
/**
 * @name getTextMaxWidth
 * @description 获取嵌入文本的最大宽度，即图形内切正方形的宽度
 * @param {Object} polygonVertices 图形坐标顶点
 * @returns {number} 图形内切正方形的宽度
 */
function getTextMaxWidth(polygonVertices) {
  // 计算两点之间的距离
  function distanceBetweenPoints(point1, point2) {
    return Math.sqrt(
      Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2)
    );
  }

  // 计算多边形的内切正方形的宽度
  function calculateInscribedSquareWidth(polygonVertices) {
    // 初始化最短边的长度为正无穷
    let minEdgeLength = Infinity;

    // 计算多边形每条边的长度，并找到最短边的长度
    for (let i = 0; i < polygonVertices.length; i++) {
      let p1 = polygonVertices[i];
      let p2 = polygonVertices[(i + 1) % polygonVertices.length];
      let edgeLength = distanceBetweenPoints(p1, p2);
      minEdgeLength = Math.min(minEdgeLength, edgeLength);
    }

    // 内切正方形的宽度即为最短边的长度
    return minEdgeLength;
  }

  // 计算内切正方形的宽度
  let squareWidth = calculateInscribedSquareWidth(polygonVertices);

  // 输出内切正方形的宽度
  console.log("内切正方形的宽度:", squareWidth);
  return squareWidth;
}

/**
 * 在 Canvas 中绘制自动折行的文本
 * @param {CanvasRenderingContext2D} context - Canvas 上下文对象
 * @param {string} text - 要绘制的文本
 * @param {number} x - 文本起始 x 坐标
 * @param {number} y - 文本起始 y 坐标
 * @param {number} maxWidth - 文本最大宽度
 * @param {number} lineHeight - 行高
 * @returns {Array} - 包含每行文本和其 Y 坐标的数组
 */
function drawWrappedText(context, text, x, y, maxWidth, lineHeight) {
  var words = text.split("");
  var line = "";
  var lines = [];
  var currentY = y;

  for (var i = 0; i < words.length; i++) {
    var testLine = line + words[i];
    var metrics = measureText(context, testLine);
    var testWidth = metrics.width;
    if (testWidth > maxWidth && i > 0) {
      lines.push({ text: line, y: currentY });
      line = words[i];
      currentY += lineHeight;
    } else {
      line = testLine;
    }
  }
  lines.push({ text: line, y: currentY });

  return lines;
}

/**
 * 在 Canvas 中绘制自动折行的文本并使其水平和垂直居中
 * @param {CanvasRenderingContext2D} context - Canvas 上下文对象
 * @param {string} text - 要绘制的文本
 * @param {number} x - 文本起始 x 坐标
 * @param {number} y - 文本起始 y 坐标
 * @param {number} maxWidth - 文本最大宽度
 * @param {number} lineHeight - 行高
 */
function drawCenteredWrappedText(context, text, x, y, maxWidth, lineHeight) {
  var lines = drawWrappedText(context, text, x, y, maxWidth, lineHeight);

  var totalHeight = lines.length * lineHeight;
  var startY = y - totalHeight / 2;

  for (var j = 0; j < lines.length; j++) {
    var line = lines[j].text;
    var metrics = measureText(context, line);
    var totalWidth = metrics.width;
    var startX = x - totalWidth / 2;
    context.fillText(line, startX, startY + j * lineHeight);
  }
}

/**
 * 测量文本的宽度
 * @param {CanvasRenderingContext2D} context - Canvas 上下文对象
 * @param {string} text - 要测量的文本
 * @returns {Object} - 包含文本宽度的对象
 */
function measureText(context, text) {
  var span = document.createElement("span");
  span.style.font = context.font;
  span.textContent = text;
  document.body.appendChild(span);
  var width = span.offsetWidth;
  document.body.removeChild(span);
  return { width: width };
}

/**
 * @name getFirstTouchShapeIndex
 * @description 获取第一个被触碰到的图标的索引
 * @param {number} x 横坐标
 * @param {number} y 纵坐标
 * @param {object} shapes 
 * @returns {number} 
 */
function getFirstTouchShapeIndex(x, y, shapes) {
  const matctedShapes = shapes.list.filter((it) => it.isInside(x, y));
  if (matctedShapes.length > 0) {
    const matctedShape = matctedShapes[matctedShapes.length - 1];
    const index = shapes.list.findIndex((item) => {
      return (
        item.startX === matctedShape.startX &&
        item.startY === matctedShape.startY
      );
    });
    return index;
  }
  return -1;
}


function isSameShape (target, original) {
  return target === original
}

// 将直线与水平轴的角度转换为斜率
function angleRadiansToSlope(angle) {
  return Math.tan(angle);
}

// 将斜率转换为直线与水平轴的弧度
function slopeToAngleRadians(slope) {
  return Math.atan(slope);
}

// 弧度转角度
function angleRadiansToAngleDegrees(angleRadians) {
  return angleRadians * (180 / Math.PI);
}

// 角度转弧度
function angleDegreesToAngleRadians(angleDegrees) {
  return angleDegrees * (Math.PI / 180);
}
